package collectdemo13.map.hashmap;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

class Cat
{
	private String name = null ;
	private int age = 0 ;
	
	public Cat(String name, int age)
	{
		super();
		this.setAge(age);
		this.setName(name);
	}
	public String getName()
	{
		return name;
	}
	public void setName(String name)
	{
		this.name = name;
	}
	public int getAge()
	{
		return age;
	}
	public void setAge(int age)
	{
		this.age = age;
	}
	@Override
	public String toString()
	{
		return "Cat [name=" + this.name + ", age=" + this.age + "]";
	}
	@Override
	public int hashCode()
	{
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj)
	{
		if(this == obj)
			return true;
		if(obj == null)
			return false;
		if(getClass() != obj.getClass())
			return false;
		Cat other = (Cat)obj;
		if(age != other.age)
			return false;
		if(name == null)
		{
			if(other.name != null)
				return false;
		}
		else if(!name.equals(other.name))
			return false;
		return true;
	} 
	
	
}
public class HashMapDemo06
{

	/**
	 * @param args
	 */
	@SuppressWarnings("unchecked")
	public static void main(String[] args)
	{
		// TODO Auto-generated method stub
		Map<Cat,String> map = new HashMap<Cat,String>();
		Map<Cat,String> mapClone = null;
		//对象克隆技术
		mapClone = (HashMap<Cat,String>)(((HashMap<Cat,String>)map).clone());
		System.out.println("输入值之前判断是否为空："+map.isEmpty()+",个数:"+map.size());
		System.out.println("输入值之前判断是否为空："+mapClone.isEmpty()+",个数:"+mapClone.size());
		//此处输出为空原因是对于map的匹配过程 有一个特点，
		//即：对象要一样才可以将内容查询出来
		//对于上面的空值 可以用下面的内容修改 改动第一处
		Cat per = new Cat("lzq",30);
		map.put(per,"1山东");
		map.put( new Cat("lzqr",32),"2江苏");
		map.put(new Cat("wgcu",30),"3浙江");
		//此处输出为空原因是对于map的匹配过程 有一个特点，
		//即：对象要一样才可以将内容查询出来
		System.out.println("改好了new Cat(\"lzq\",30)----"+map.get(new Cat("lzq",30)));
		//对于上面的空值 可以用下面的内容修改改动第二处
		
		//法一输出方式entrySet() + iterator
		System.out.println("new Cat(\"lzq\",30)----"+map.get(per));
		System.out.println("法一输出：");
		Set<Map.Entry<Cat,String>> set= map.entrySet();
		Iterator<Map.Entry<Cat,String>> iter = set.iterator();
		while(iter.hasNext())
		{
			Map.Entry<Cat,String> hs = (Map.Entry<Cat,String>)iter.next();
			System.out.println(hs.getKey()+"----------->"+hs.getValue());
		}
		//法二输出方式entrySet() + foreach
		System.out.println("法二输出：");
		for(Map.Entry<Cat,String> me : map.entrySet())
		{
			System.out.println(me.getKey()+"----------->"+me.getValue());
		}	
	}

}
